package com.lkd.handler;
import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.StatusInfo;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
@Slf4j
public class VMStatusHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        log.info("采集到状态数据");
        var vmStatusContract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        if(vmStatusContract == null || vmStatusContract.getStatusInfo() == null || vmStatusContract.getStatusInfo().size() <= 0) return;
        //list集合转换为map
        Map<String,Boolean> statusMap= vmStatusContract.getStatusInfo().stream().collect(Collectors.toMap( StatusInfo::getStatusCode,StatusInfo::isStatus ));

        vendingMachineService.updateStatusMap(vmStatusContract.getInnerCode(),statusMap);  //更新状态
    }
}